package com.four.bmm.service;

import com.four.bmm.dao.IBlockDao;
import com.four.bmm.entity.Block;
import com.four.bmm.entity.BlockItem;
import com.four.bmm.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class BlockService {
    SqlSession session = SqlSessionFactoryUtil.getSqlSession();
    IBlockDao blockDao = session.getMapper(IBlockDao.class);

    /**
     * 查询所有一级版块
     */
    public List<Block> findAllBlock() {
        return blockDao.findAllBlock();
    }

    /**
     * 查询所有二级版块
     */
    public List<BlockItem> findAllBItem() {
        return blockDao.findAllBlockItem();
    }

    /**
     * 计算一级版块总数量
     */
    public int calBlockCount() {
        return blockDao.calBlockCount();
    }

    /**
     * 计算二级版块总数量
     */
    public int calBlockItemCount() {
        return blockDao.calBlockItemCount();
    }

    /**
     * 添加版块
     */
    public boolean addBlock(Block block) {
        boolean result = false;
        try {
            result = blockDao.addBlock(block);
            session.commit();//提交
        } catch (Exception e) {
            session.close();
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 添加二级版块
     *
     * @param blockItem
     * @return
     */
    public boolean addBlockItem(BlockItem blockItem) {
        boolean result = false;
        try {
            result = blockDao.addBlockItem(blockItem);
            session.commit();//提交
        } catch (Exception e) {
            session.close();
            e.printStackTrace();
        }
        return result;

    }

    /**
     * 删除版块
     */
    public boolean delBlock(int blockid) {
        boolean flag = false;
        try {
            flag = blockDao.delBlock(blockid);
            session.commit();
        } catch (Exception e) {
            session.close();
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除二级版块
     *
     * @param bitemid
     * @return
     */
    public boolean delBlockItem(int bitemid) {
        boolean flag = false;
        try {
            flag = blockDao.delBlockItem(bitemid);
            session.commit();
        } catch (Exception e) {
            session.close();
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 编辑版块
     */
    public boolean editBlock(Block block) {
        boolean flag = false;
        try {
            flag = blockDao.editBlock(block);
            session.commit();
        } catch (Exception e) {
            session.close();
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 通过一级版块di获取其下的二级版块
     * @param blockid
     * @return
     */
    public List<BlockItem> getAllBlockItem(int blockid){
        return blockDao.getAllBlockItem(blockid);
    }
}
